<?php 
class cls_control_customer extends cls_control_base
{
	function login()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		$email = $_POST['user']['email'];
		$password = $_POST['user']['password'];
		$remember_user = $_POST['remember_user'];
		$validate_input = $_POST['verify'];
		
		$email = mysql_real_escape_string($email);
		$password = mysql_real_escape_string($password);
		$remember_user = mysql_real_escape_string($remember_user);
		
		if(cls_control_base::check_if_empty($email))
		{
			$message = return_info(false, "邮箱不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($password))
		{
			$message = return_info(false, "密码不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($validate_input))
		{
			$message = return_info(false, "验证码不能为空");
			echo json_encode($message);
			return;
		}

		$result = $customer_serve->login($email,$password,$remember_user,$validate_input);
		echo json_encode($result);
	}
	
	function logoff()
	{
		setcookie('b2c_customerid', "", time()-3600);
		unset($_SESSION['user']);
	}
	
	function register()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		$name = $_POST['user']['name'];
		$password = $_POST['user']['password'];
		$email = $_POST['user']['email'];
		$birthday = $_POST['birthday'];
		$location = $_POST['location'];
		$password_confirm = $_POST['user']['password_confirmation'];
		$validate_input = $_POST['verify'];
		
		$name = mysql_real_escape_string($name);
		$password = mysql_real_escape_string($password);
		$email = mysql_real_escape_string($email);
		$birthday = mysql_real_escape_string($birthday);
		$location = mysql_real_escape_string($location);
		
		if($password != $password_confirm)
		{
			$message = return_info(false, "两次输入密码不一致");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($name))
		{
			$message = return_info(false, "姓名不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($email))
		{
			$message = return_info(false, "邮箱不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($password))
		{
			$message = return_info(false, "密码不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($validate_input))
		{
			$message = return_info(false, "验证码不能为空");
			echo json_encode($message);
			return;
		}
		if(strlen($name)<2 || strlen($name)>20)
		{
			$message = return_info(false, "姓名长度必须在2~20个字之间");
			echo json_encode($message);
			return;
		}
		if(preg_match("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)) {
			$message = return_info(false, "请输入正确的邮件地址");
			echo json_encode($message);
			return;
		}
		if(strlen($password)<6 || strlen($password)>20)
		{
			$message = return_info(false, "密码长度必须在6~20位之间");
			echo json_encode($message);
			return;
		}
		$has_letter = false;
		$has_number = false;
		$other_letter = false;
		$array = str_split($password,1);
		for($i=0;$i<strlen($password);$i++)
		{
			if(ctype_alpha($array[$i]))
			{
				$has_letter = true;
			}
			else if(is_numeric($array[$i]))
			{
				$has_number = true;
			}
			else
			{
				$other_letter = true;
			}
		}
		if(!($has_letter && $has_number && !$other_letter))
		{
			$message = return_info(false, "密码必须为字母与数字的组合");
			echo json_encode($message);
			return;
		}
		
		$result = $customer_serve->register($name, $password, $email, $birthday, $location, $validate_input);
		echo json_encode($result);
	}
	
	function check_exist_email()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		$email = $_REQUEST['email'];
		
		$email = mysql_real_escape_string($email);
		
		if(cls_control_base::check_if_empty($email))
		{
			$message = return_info(false, "邮箱不能为空");
			echo json_encode($message);
			return;
		}
		
		$check_result = $customer_serve->check_email_exist($email);
		echo json_encode($check_result);
	}
	
	function get_customer_inofrmation()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登陆超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		
		$customer_information = $customer_serve->get_customer_information($customerid);
		echo json_encode($customer_information);
	}
	
	function update_customer_information()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登陆超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$name = $_POST['user']['name'];
		$birthday = $_POST['user']['birthday'];
		$location = $_POST['user']['location'];
		
		if(cls_control_base::check_if_empty($name))
		{
			$message = return_info(false, "姓名不能为空");
			echo json_encode($message);
			return;
		}
		if(strlen($name)<2 || strlen($name)>20)
		{
			$message = return_info(false, "姓名长度必须在2~20个字符之间");
			echo json_encode($message);
			return;
		}
		
		$name = mysql_real_escape_string($name);
		$birthday = mysql_real_escape_string($birthday);
		$location = mysql_real_escape_string($location);
		
		$result = $customer_serve->update_customer_information($customerid, $name, $birthday, $location);
		echo json_encode($result);
	}
	
	function update_customer_password()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登陆超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$old_password = $_POST['user']['current_password'];
		$new_password = $_POST['user']['password'];
		$password_confirm = $_POST['user']['password_confirmation'];
		
		$old_password = mysql_real_escape_string($old_password);
		$new_password = mysql_real_escape_string($new_password);
		
		if($new_password != $password_confirm)
		{
			$message = return_info(false, "两次输入密码不一致");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($old_password))
		{
			$message = return_info(false, "密码不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($new_password))
		{
			$message = return_info(false, "密码不能为空");
			echo json_encode($message);
			return;
		}
		if(strlen($new_password)<6 || strlen($new_password)>20)
		{
			$message = return_info(false, "密码长度必须在6~20位之间");
			echo json_encode($message);
			return;
		}
		$has_letter = false;
		$has_number = false;
		$other_letter = false;
		$array = str_split($new_password,1);
		for($i=0;$i<strlen($new_password);$i++)
		{
			if(ctype_alpha($array[$i]))
			{
				$has_letter = true;
			}
			else if(is_numeric($array[$i]))
			{
				$has_number = true;
			}
			else
			{
				$other_letter = true;
			}
		}
		if(!($has_letter && $has_number && !$other_letter))
		{
			$message = return_info(false, "密码必须为字母与数字的组合");
			echo json_encode($message);
			return;
		}
		
		$old_md5pwd = md5($old_password);
		$new_md5pwd = md5($new_password);
		
		$result = $customer_serve->update_customer_password($customerid, $old_md5pwd, $new_md5pwd);
		echo json_encode($result);
	}
	
	function add_pwd_reset_verify()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);

		$email = $_POST['email'];
		if(cls_control_base::check_if_empty($email))
		{
			$message = return_info(false, "邮箱不能为空");
			echo json_encode($message);
			return;
		}
		$email = mysql_real_escape_string($email);

		 $result = $customer_serve->add_pwd_reset_verify($email);
		 echo json_encode($result);
	}

	function reset_password()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		
		$customerid = $_POST['customerid'];
		$password = $_POST['user']['password'];
		$password_confirm = $_POST['user']['password_confirmation'];
		$verifycode = $_POST['verifycode'];
		if(cls_control_base::check_if_empty($password))
		{
			$message = return_info(false, "密码不能为空");
			echo json_encode($message);
			return;
		}
		if($password != $password_confirm)
		{
			$message = return_info(false, "两次输入密码不一致");
			echo json_encode($message);
			return;
		}
		if(strlen($password)<6 || strlen($password)>20)
		{
			$message = return_info(false, "密码长度必须在6~20位之间");
			echo json_encode($message);
			return;
		}
		$has_letter = false;
		$has_number = false;
		$other_letter = false;
		$array = str_split($password,1);
		for($i=0;$i<strlen($password);$i++)
		{
			if(ctype_alpha($array[$i]))
			{
				$has_letter = true;
			}
			else if(is_numeric($array[$i]))
			{
				$has_number = true;
			}
			else
			{
				$other_letter = true;
			}
		}
		if(!($has_letter && $has_number && !$other_letter))
		{
			$message = return_info(false, "密码必须为字母与数字的组合");
			echo json_encode($message);
			return;
		}

		$customerid = mysql_real_escape_string($customerid);
		$password = mysql_real_escape_string($password);

		$result = $customer_serve->reset_password($customerid, $password, $verifycode);
		echo json_encode($result);
	}
	
	function active()
	{
		$customer_serve = cls_serve_base::get_instance($this->name);
		$customerid = $_REQUEST['customerid'];
		$verify_code = $_REQUEST['verifycode'];
		
		$customerid = mysql_real_escape_string($customerid);
		$verify_code = mysql_real_escape_string($verify_code);
		
		$result = $customer_serve->verify_email($customerid, $verify_code);
		
		if($result['success'])
		{
			echo('<html>
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			</head>
			<div id="timeClew"></div>
			<script type="text/javascript">
			var url = "http://dev.b2ca.com/" //要跳转的地址
			var obj = document.getElementById("timeClew"), time = 3;
			function setTimeClew(){
			 obj.innerHTML = "验证成功，" + (time--) + "秒后自动跳转，如果没有自动跳转<a href=\"" + url + "\">请点这里<\/a>";
			 if(time < 0){ window.location.href = url; }else{ setTimeout(setTimeClew, 1000) }
			}
			setTimeClew();
			</script>
			</html>');
		}
		else
		{
			echo('<html>
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			</head>
			<div id="timeClew"></div>
			<script type="text/javascript">
			var url = "http://dev.b2ca.com/" //要跳转的地址
			var obj = document.getElementById("timeClew"), time = 3;
			function setTimeClew(){
			 obj.innerHTML = "激活验证码错误，" + (time--) + "秒后自动跳转，如果没有自动跳转<a href=\"" + url + "\">请点这里<\/a>";
			 if(time < 0){ window.location.href = url; }else{ setTimeout(setTimeClew, 1000) }
			}
			setTimeClew();
			</script>
			</html>');
		}
	}
}
?>